<?php
/**
 *
 * @author ninteySeven.
 * @date 2022-12-12 0:32
 */

if (!function_exists('success')) {
    function success($data = [])
    {
        $headers = array(
            'Content-Type' => 'application/json; charset=utf-8'
        );

        $result = [
            'code' => 200,
            'message' => 'success',
            'data' => !empty($data) ? $data : [],
        ];

        return response()->json($result,200,$headers,JSON_UNESCAPED_UNICODE);
    }
}

if (!function_exists('fail')) {
    function fail($code, $message, $data = [])
    {
        $headers = array(
            'Content-Type' => 'application/json; charset=utf-8'
        );
        $result = [
            'code' => $code,
            'message' => $message,
            'data' => !empty($data) ? $data : [],
        ];

        return response()->json($result,200,$headers,JSON_UNESCAPED_UNICODE);
    }
}

if (!function_exists('randSmsCode')) {
    function randSmsCode()
    {
        $chars = '0123456789';
        $code = '';
        while (strlen($code) < 4) {
            $code .= substr($chars, (mt_rand() % strlen($chars)), 1);
        }
        return $code;
    }
}

if (!function_exists('timeDiff')) {
    function timeDiff($begin_time, $end_time)
    {
        if ($begin_time < $end_time) {
            $starttime = $begin_time;
            $endtime = $end_time;
        } else {
            $starttime = $end_time;
            $endtime = $begin_time;
        }

        //计算天数
        $timediff = $endtime - $starttime;
        $days = intval($timediff / 86400);
        //计算小时数
        $remain = $timediff % 86400;
        $hours = intval($remain / 3600);
        //计算分钟数
        $remain = $remain % 3600;
        $mins = intval($remain / 60);
        //计算秒数
        $secs = $remain % 60;
        $res = array("day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs);
        return $res;
    }
}

if (!function_exists('md5Password')) {
    function md5Password($password): string
    {
        $halt = 'sxcx2023';
        return md5($halt . sha1($password));
    }
}

if (!function_exists('curlGet')) {
    /**
     *模拟GET请求
     **/
    function curlGet($url)
    {
        $headerArray = array("Content-type:application/json;", "Accept:application/json");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output, true);
        return $output;
    }
}

if (!function_exists('curlPost')) {
    /**
     * CURL的Post请求方法
     * @param string $url
     * @param string $param
     * @return bool|string
     */
    function curlPost(string $url = '', string $param = '')
    {
        if (empty($url) || empty($param)) {
            return false;
        }

        $postUrl = $url;
        $curlPost = $param;
        // 初始化curl
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $postUrl);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        // post提交方式
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        // 运行curl
        $data = curl_exec($curl);
        curl_close($curl);

        return $data;
    }
}
